#include "saveasdialog.h"

SaveAsDialog::SaveAsDialog( QString file_name,
                            QString last_dir )
{
setupUi(this);

QFileInfo fi(file_name);
QString name = fi.fileName();

    if ( true == last_dir.isEmpty() )
    {
    folder_name = QDir::homePath();
    }
    else
    {
    QDir test_dir(last_dir);
        if ( true == test_dir.isReadable() )
        {
        folder_name = last_dir;
        }
        else
        {
        folder_name = QDir::homePath();
        }
    }
dirLineEdit->setText( QDir::toNativeSeparators(folder_name) );

ext = name.section(".", -1);
base_name = name.section(".", 0, -2);
QString no_name = QLatin1String("noname");    
    if ( base_name.isEmpty() )
    {
    base_name = no_name;
    }

QString point = QLatin1String("");
    if ( !ext.isEmpty() )
    {
    point = QLatin1String(".");
    }

fLineEdit->setText( base_name + point + ext );
formatComboBox->insertItems( 0, get_formats() );
connect( formatComboBox,
         SIGNAL( activated(const QString &) ),
         this,
         SLOT( select_format(const QString &) ) );
connect( dirButton,
         SIGNAL( clicked() ),
         this,
         SLOT( set_folder() ) );
}

SaveAsDialog::~SaveAsDialog()
{
}

QStringList SaveAsDialog::get_formats()
{
QList<QByteArray> f = QImageWriter::supportedImageFormats();
QStringList l_check;
    for (int i = 0; i< f.size(); ++i)
    {
    l_check << QString( f.at(i) );
    }
    
QStringList l;
    for (int i = 0; i< f.size(); ++i)
    {
        if ( f.at(i) != ext)
        {
        l << QString( f.at(i) );
        }
    }    

    if ( true == l_check.contains(ext, Qt::CaseInsensitive) )
    {
    l.prepend(ext);
    }
    else
    {
    l.prepend("");
    }

return l;
}

void SaveAsDialog::select_format(const QString & format)
{
fLineEdit->setText( base_name + "." + format );
}

QMap<QString, QVariant> SaveAsDialog::return_data()
{
QMap<QString, QVariant> m;
QString new_file = QLatin1String("");
new_file = fLineEdit->text();
m["new_file"] = new_file;
m["folder_name"] = folder_name;
    if ( true == formatGroupBox->isChecked() )
    {
        if ( ext != new_file.section(".", -1) )
        {
        m["format_changed"] = QLatin1String("T");
        m["new_format"] = new_file.section(".", -1);
        }
        else
        {
        m["format_changed"] = QLatin1String("F");
        m["new_format"] = QLatin1String("");
        }

        if ( true != comprCheckBox->isChecked() )
        {
        m["compr_changed"] = QLatin1String("T");
        m["new_quality"] = compSlider->value();
        }
        else
        {
        m["compr_changed"] = QLatin1String("F");
        m["new_quality"] = -1;
        }
    }
    else
    {
    m["format_changed"] = QLatin1String("F");
    m["new_format"] = QLatin1String("");
    m["new_quality"] = -1;
    }

    if (true == resizeGroupBox->isChecked() )
    {
    m["resize"] = QLatin1String("T");
    m["side"] = resizeComboBox->currentText();
    m["size"] = resizeSpinBox->value();
    }
    else
    {
    m["resize"] = QLatin1String("F");
    m["side"] = QLatin1String("");
    m["size"] = -1;
    }

return m;
}

void SaveAsDialog::set_folder()
{
QString folder_name_ = QFileDialog::getExistingDirectory
              (this,
              tr("Directory"),
              folder_name,
              QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly);
folder_name = folder_name_;
dirLineEdit->setText( QDir::toNativeSeparators(folder_name) );
}
